package com.zto.tbschedule.console.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import com.zto.tbschedule.console.mapper.AppUserModelMapper;
import com.zto.tbschedule.console.mapper.ApplicationModelMapper;
import com.zto.tbschedule.console.mapper.UserModelMapper;
import com.zto.tbschedule.console.model.*;
import com.zto.tbschedule.console.service.IApplicationService;
import com.zto.tbschedule.console.vo.AppUser;
import com.zto.tbschedule.console.vo.Application;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApplicationServiceImpl implements IApplicationService {
    @Autowired
    private ApplicationModelMapper appMapper;
    @Autowired
    private AppUserModelMapper appUserMapper;
    @Autowired
    private UserModelMapper userMapper;
    public List<Application> getAppList() {
        List<Application> applicationList = new ArrayList<Application>();

            List<ApplicationModel> applicationModelList = appMapper.selectByExample(new ApplicationModelExample());
            if(!CollectionUtils.isEmpty(applicationModelList)){
                for (ApplicationModel applicationModel:applicationModelList){
                    Application application = appModel2App(applicationModel);
                    if(application.getCreateId()!=null) {
                        UserModel createUser = userMapper.selectByPrimaryKey(application.getCreateId());
                        application.setCreateMan(createUser != null ? createUser.getNickName() : null);
                    }
                    if(application.getModifyId()!=null){
                        UserModel modifyUser = userMapper.selectByPrimaryKey(application.getModifyId());
                        application.setModifyMan(modifyUser != null ? modifyUser.getNickName() : null);
                    }
                    applicationList.add(application);
                }
            }

        return applicationList;
    }

    private boolean isMyApp(Long userId, Long id) {
        AppUserModelExample example = new AppUserModelExample();
        example.createCriteria().andUidEqualTo(userId).andAidEqualTo(id);
        int count = appUserMapper.countByExample(example);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }

    public static Application appModel2App(ApplicationModel applicationModel) {
        if (applicationModel == null) {
            return null;
        }
        Application application = new Application();
        application.setId(applicationModel.getId());
        application.setCode(applicationModel.getCode());
        application.setName(applicationModel.getName());
        application.setDescription(applicationModel.getDescription());
        application.setZkPath(applicationModel.getZkPath());
        application.setZkUserName(applicationModel.getZkUsername());
        application.setZkPassWord(applicationModel.getZkPassword());
        application.setCreateDate(applicationModel.getCreateDate());
        application.setCreateId(applicationModel.getCreateId());
        application.setModifyDate(applicationModel.getModifyDate());
        application.setModifyId(applicationModel.getModifyId());
        return application;
    }

    public void addApp(Application app) {
        ApplicationModel applicationModel = app2AppModel(app);
        Assert.notNull(applicationModel, "实体转换异常");
        try {
            appMapper.insertSelective(applicationModel);
            ApplicationModelExample example = new ApplicationModelExample();
            example.createCriteria().andCodeEqualTo(applicationModel.getCode());
            List<ApplicationModel> applicationModels = appMapper.selectByExample(example);
            AppUserModel appUserModel = new AppUserModel();
            appUserModel.setAid(applicationModels.get(0).getId());
            appUserModel.setUid(app.getCreateId());
            appUserMapper.insertSelective(appUserModel);
        }catch (Exception e){
            throw new RuntimeException("保存异常",e);
        }
    }

    public List<AppUser> getAppListByUid(Long userId) {
        List<AppUser> appUserList = new ArrayList<AppUser>();
        List<Application> applicationList = getAppList();
        if(!CollectionUtils.isEmpty(applicationList)){
            for (Application app :applicationList){
                AppUser appUser = new AppUser();
                appUser.setApp(app);
                appUser.setUserId(userId);
                appUser.setFlag(isMyApp(userId, app.getId()));
                appUserList.add(appUser);
            }
        }
        return appUserList;
    }

    public void updateMyApp(Long uid, Long aid, String type) {
        if("ADD".equals(type)){
            AppUserModel appUserModel = new AppUserModel();
            appUserModel.setAid(aid);
            appUserModel.setUid(uid);
            appUserMapper.insertSelective(appUserModel);
        }else if("DEL".equals(type)){
            AppUserModelExample example = new AppUserModelExample();
            example.createCriteria().andAidEqualTo(aid).andUidEqualTo(uid);
            appUserMapper.deleteByExample(example);
        }else{
            throw new RuntimeException("未知的操作类型");
        }
    }

    public static ApplicationModel app2AppModel(Application app) {
        if (app == null) {
            return null;
        }
        ApplicationModel applicationModel = new ApplicationModel();
        applicationModel.setId(app.getId());
        applicationModel.setCode(app.getCode());
        applicationModel.setName(app.getName());
        applicationModel.setDescription(app.getDescription());
        applicationModel.setZkPath(app.getZkPath());
        applicationModel.setZkUsername(app.getZkUserName());
        applicationModel.setZkPassword(app.getZkPassWord());
        applicationModel.setCreateId(app.getCreateId());
        return applicationModel;
    }
}
